package com.ssyt.store.sys.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssyt.store.common.PageModel;
import com.ssyt.store.common.util.StringUtil;
import com.ssyt.store.sys.entity.StoreCart;
import com.ssyt.store.sys.entity.StoreOrder;
import com.ssyt.store.sys.entity.StoreOrderProduct;
import com.ssyt.store.sys.entity.StoreReceiveAddress;
import com.ssyt.store.sys.entity.StoreTicket;
import com.ssyt.store.sys.entity.SysUserEntity;
import com.ssyt.store.sys.entity.temp.StoreOrderProductModel;
import com.ssyt.store.sys.service.StoreOrderProductService;
import com.ssyt.store.sys.service.StoreOrderService;
import com.ssyt.store.sys.service.StoreReceiveAddressService;
import com.ssyt.store.sys.service.StoreShoppingCartService;
import com.ssyt.store.sys.service.StoreTicketService;
import com.ssyt.store.sys.service.SysLogService;
import com.ssyt.store.sys.service.SysUserRoleService;
import com.ssyt.store.sys.service.SysUserService;

@Controller
@RequestMapping("/store/shopping")
public class ShoppingController {
	@Autowired
	private StoreShoppingCartService storeShoppingCartService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private StoreOrderProductService storeOrderProductService;
	@Autowired
	private StoreReceiveAddressService storeReceiveAddressService;
	@Autowired
	private StoreOrderService storeOrderService;
	@Autowired
	private StoreTicketService storeTicketService;
	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	private SysLogService sysLogService;
	
	
	
	
	
	
	
	
	@RequestMapping("/listShoppingCart")
	public String listShoppingCart(Model model,HttpSession session,Integer pageNo){
		if(pageNo == null){
			pageNo = 1;
		}
		SysUserEntity user = (SysUserEntity)session.getAttribute("user");
		String id = user.getId();
		PageModel<StoreCart> products = storeShoppingCartService.list(pageNo, id);
		model.addAttribute("products",products);
		return "store/shopcar";
	}
	//
	@RequestMapping("/login")
	public String login(SysUserEntity user, HttpSession session,Model model) {
		SysUserEntity sysUserEntity = sysUserService.login(user.getCode(),
				user.getPassword());
		if (sysUserEntity != null) {
			session.setAttribute("user", sysUserEntity);
			String RoleId = sysUserRoleService.getRoleByUserId(sysUserEntity.getId());
			if("sr-002".equals(RoleId)){
				return "redirect:/shopping/product/showAllProducts.action";
			}else{
				sysLogService.addLogs("登录", sysUserEntity.getCode());
				return "sys/index";
			}
		}else{
			model.addAttribute("loginFlag","fail");
			return "redirect:/shopping/product/showAllProducts.action";
		}
	}
	
	@RequestMapping("/regsUser")
	public String regsUser(SysUserEntity user,Model model){
		sysUserService.addUser(user);
		model.addAttribute("loginFlag","success");
		return "redirect:/shopping/product/showAllProducts.action";
	}
	
	@RequestMapping("/addToCart")
	@ResponseBody
	public String addProduct(String productId,int count, HttpSession session) {
		SysUserEntity user = (SysUserEntity)session.getAttribute("user");
		String id = user.getId();
		storeShoppingCartService.addStoreCart(id,productId,count);
		return "store/info";
	}
	
	@RequestMapping("/deleteTheShopcar")
	public String deleteTheShopcar(String productId,Model model){
		storeShoppingCartService.deleteShoppingCartById(productId);
		return "redirect:/store/shopping/listShoppingCart.action";
	}
	@RequestMapping("/deleteAllShopcar")
	public String deleteAllShopcar(Model model,HttpSession session){
		SysUserEntity user = (SysUserEntity)session.getAttribute("user");
		storeShoppingCartService.deleteAllShoppingCartByUserId(user.getId());
		return "redirect:/store/shopping/listShoppingCart.action";
	}
	
	@RequestMapping("/addToOrder")
	public String addToOrder(StoreOrderProductModel storeOrderProductModel,HttpSession session,Model model) {
		List<StoreOrderProduct> products = storeOrderProductModel.getStoreOrderProducts();
		SysUserEntity user = (SysUserEntity)session.getAttribute("user");
		String userId = user.getId();
		BigDecimal allTotalPrice = new BigDecimal(0);
		for(int i = 0; i < products.size(); i++){
			StoreOrderProduct storeOrderProduct = products.get(i);
			storeOrderProduct.setState((byte) 1);
			allTotalPrice = allTotalPrice.add(storeOrderProduct.getSubtotal());
		}
		storeShoppingCartService.deleteAllShoppingCartByUserId(userId);
		String ids = storeOrderProductService.addStoreOrderProduct(products, userId);
		List<StoreReceiveAddress> receiveAddresses = storeReceiveAddressService.getAddressByUserId(userId);
		model.addAttribute("receiveAddresses", receiveAddresses);
		model.addAttribute("products",products);
		model.addAttribute("productsIds", ids);
		model.addAttribute("allTotalPrice",allTotalPrice);
		return "store/orderlist";
	}

	@RequestMapping("/addNewAddress")
	@ResponseBody
	public StoreReceiveAddress addNewAddress(StoreReceiveAddress storeReceiveAddress,HttpSession session){
		SysUserEntity user = (SysUserEntity)session.getAttribute("user");
		String userId = user.getId();
		storeReceiveAddress.setSysUserId(userId);
		storeReceiveAddress.setState((byte)1);
		storeReceiveAddressService.addStoreReceiveAddress(storeReceiveAddress);
		return storeReceiveAddress;
	}
	
	@RequestMapping("/payForOrder")
	@ResponseBody
	public String payForOrder(String isNeedInvoice,String addressId,String storeOrderProductId,String msg,BigDecimal total,StoreTicket storeTicket,Model model,HttpSession session){
		SysUserEntity user = (SysUserEntity)session.getAttribute("user");
		String userId = user.getId();
		StoreReceiveAddress storeReceiveAddress = storeReceiveAddressService.getAddressById(addressId);
		String storeTicketId = null;
		if(isNeedInvoice != null && isNeedInvoice != ""){
			storeTicketId = storeTicketService.addStoreTicket(storeTicket);
		}
		
		StoreOrder storeOrder = new StoreOrder();
		storeOrder.setSysUserId(userId);
		storeOrder.setReceiver(storeReceiveAddress.getReceiver());
		storeOrder.setProvince(storeReceiveAddress.getProvince());
		storeOrder.setCity(storeReceiveAddress.getCity());
		storeOrder.setArea(storeReceiveAddress.getArea());
		storeOrder.setAddress(storeReceiveAddress.getAddress());
		storeOrder.setPhone(storeReceiveAddress.getPhone());
		storeOrder.setTotal(total);
		storeOrder.setMsg(msg);
		storeOrder.setTicket(storeTicketId);
		storeOrder.setStoreOrderProductId(storeOrderProductId);
		storeOrder.setPayState((byte)1);
		storeOrder.setState((byte)1);
		String orderId = storeOrderService.addStoreOrder(storeOrder);
		return orderId;
	}
	
	@RequestMapping("/goToUserInfoPage")
	public String goToUserInfoPage(Model model,HttpSession session){
		SysUserEntity user = (SysUserEntity)session.getAttribute("user");
		String userId = user.getId();
		model.addAttribute("userDefualtAddress",storeReceiveAddressService.getUserDefualtAddress(userId));
		return "store/userinfo";
	}
	
	@RequestMapping("/listReseiveAddress")
	public String listReseiveAddress(Model model,HttpSession session){
		SysUserEntity user = (SysUserEntity)session.getAttribute("user");
		String userId = user.getId();
		List<StoreReceiveAddress> receiveAddresses = storeReceiveAddressService.getAddressByUserId(userId);
		model.addAttribute("receiveAddresses",receiveAddresses);
		return "store/address";
	}
	
	@RequestMapping("/addOrUpdateAddress")
	public String addOrUpdateAddress(StoreReceiveAddress storeReceiveAddress,Model model,HttpSession session){
		SysUserEntity user = (SysUserEntity)session.getAttribute("user");
		String userId = user.getId();
		storeReceiveAddress.setSysUserId(userId);
		storeReceiveAddressService.saveOrUpdateAddress(storeReceiveAddress);
		return "redirect:/store/shopping/listReseiveAddress.action";
	}
	
	@RequestMapping("/deleteReceiveAddress")
	public String deleteReceiveAddress(String addressId,Model model){
		storeReceiveAddressService.deleteStoreReceiveAddress(addressId);
		return "redirect:/store/shopping/listReseiveAddress.action";
	}
	
	@RequestMapping("/useAddressToDefault")
	public String useAddressToDefault(String addressId,Model model,HttpSession session){
		SysUserEntity user = (SysUserEntity)session.getAttribute("user");
		String userId = user.getId();
		storeReceiveAddressService.useAddressToDefault(addressId, userId);
		return "redirect:/store/shopping/listReseiveAddress.action";
	}
	
	@RequestMapping("/listOrder")
	public String listOrder(Model model,HttpSession session){
		SysUserEntity user = (SysUserEntity)session.getAttribute("user");
		String userId = user.getId();
		List<StoreOrder> orders = storeOrderService.getStoreOrder(userId);
		if(orders != null){
			for (StoreOrder storeOrder : orders) {
				List<StoreOrderProduct> storeOrderProducts = new ArrayList<StoreOrderProduct>();
				String[] storeProductsIds = storeOrder.getStoreOrderProductId().split(",");
				for (int i = 0; i < storeProductsIds.length; i++) {
					StoreOrderProduct storeOrderProduct =storeOrderProductService.getStoreOrderProduct(storeProductsIds[i]);
					storeOrderProducts.add(storeOrderProduct);
				}
				storeOrder.setOrderProducts(storeOrderProducts);
				String ticketId = storeOrder.getTicket();
				if(StringUtil.isNotEmpty(ticketId)){
					StoreTicket ticket = storeTicketService.getStoreTicket(ticketId);
					storeOrder.setStoreTicket(ticket);
				}
				
			}
		}
		model.addAttribute("orders",orders);
		return "store/userorderlist";
	}
	
	@RequestMapping("/receiveProduct")
	public String receiveProduct(Model model,String orderId){
		storeOrderService.receiveProduct(orderId);
		return "redirect:/store/shopping/listOrder.action";
	}
	
	@RequestMapping("/deleteOrder")
	public String deleteOrder(Model model,String orderId){
		storeOrderService.deleteStoreOrder(orderId);
		return "redirect:/store/shopping/listOrder.action";
	}
	
	
}
